package com.lzx.adx.report.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.dto.TencentDspExtend;
import com.lzx.adx.report.model.huawei.request.HwReportRequest;
import com.lzx.adx.report.model.qtt.QttRequest;
import com.lzx.adx.report.model.sfkj.SfkjRequest;
import com.lzx.adx.report.model.tencent.request.TencentReportRequest;
import com.lzx.adx.report.model.toutiao.request.TouTiaoRequest;
import com.lzx.adx.report.model.vivo.ViVoRequest;
import com.lzx.adx.report.model.wifi.WifiRequest;
import com.lzx.adx.report.model.youyou.YouYouRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.*;

@Slf4j
public class UrlUtil {

    /**
     * URL 进行宏替换
     * @param reportDTO 请求参数
     * @return 返回替换后的完整url
     */
    public static String handXhsReplace( ReportDTO reportDTO,AccountDTO accountDTO) {
        long time = System.currentTimeMillis();
        Map<String,String> map=new LinkedHashMap<>();

        map.put("platform","ANDROID");
        map.put("ts_ms",String.valueOf(time));
        map.put("click_id",reportDTO.getBizRequestId());
        map.put("ip", reportDTO.getIp());
        map.put("oaid",reportDTO.getOaid());

        if(StringUtils.isNotBlank(reportDTO.getOaid())) {
            map.put("oaid_sum", SecureUtil.md5(reportDTO.getOaid()));
        }
        map.put("android_id_sum","");

        if(StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            map.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }

        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        log.info("完整的回调地址{}",allCabackUrl);
        map.put("callback_url",allCabackUrl);

        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        return  accountDTO.getDspReportUrl()+"?"+params;
    }


    /**
     * URL 进行宏替换
     * @param reportDTO 请求参数
     * @return 返回替换后的完整url
     */
    public static String handXhsIosReplace(ReportDTO reportDTO,AccountDTO accountDTO) {
        long time = System.currentTimeMillis();
        Map<String,String> map=new LinkedHashMap<>();

        map.put("platform","IOS");
        map.put("ts_ms",String.valueOf(time));
        map.put("click_id",reportDTO.getBizRequestId());
        map.put("ip", reportDTO.getIp());
        if (StringUtils.isNotBlank(reportDTO.getOaid())){
            if (reportDTO.getOaid().contains(",")){
                String[] split = reportDTO.getOaid().split(",");
                map.put("caid0",split[0]);
                map.put("caid1",split[1]);
            }else {
                map.put("caid0",reportDTO.getOaid());
            }
        }
        if(StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            map.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        log.info("完整的爱奇艺到小红书ios的回调地址{}",allCabackUrl);
        map.put("callback_url",allCabackUrl);
        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        return  accountDTO.getDspReportUrl()+"?"+params;
    }



    /**
     * URL 进行宏替换
     * @param reportDTO 请求参数
     * @return 返回替换后的完整url
     */
    public static String handJdjrReplace( ReportDTO reportDTO,AccountDTO accountDTO) {
        long time = System.currentTimeMillis();
        Map<String,String> map=new LinkedHashMap<>();

        map.put("platform","android");
        map.put("ts_ms",String.valueOf(time));
        map.put("click_id",reportDTO.getBizRequestId());
        map.put("ip", reportDTO.getIp());
        map.put("oaid",reportDTO.getOaid());

        if(StringUtils.isNotBlank(reportDTO.getOaid())) {
            map.put("oaid_sum", SecureUtil.md5(reportDTO.getOaid()));
        }
        map.put("android_id_sum","");

        if(StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            map.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }

        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        log.info("完整的回调地址{}",allCabackUrl);
        map.put("callback_url",allCabackUrl);

        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        return  accountDTO.getDspReportUrl()+"&"+params;
    }


    /**
     * URL 进行宏替换
     * @param reportDTO 请求参数
     * @return 返回替换后的完整url
     */
    public static String handGDTJdjrReplace( ReportDTO reportDTO,AccountDTO accountDTO) {
        long time = System.currentTimeMillis();
        Map<String, String> map = new LinkedHashMap<>();
        map.put("platform", "android");
        map.put("ts_ms", String.valueOf(time));
        map.put("click_id", reportDTO.getBizRequestId());
        map.put("ip", reportDTO.getIp());
        map.put("oaid_sum", reportDTO.getOaid());
        map.put("android_id_sum", reportDTO.getHash_android_id());
        map.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());

        map.put("callback_url", allCabackUrl);
        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        String result = accountDTO.getDspReportUrl() + "&" + params;
        log.info("完整的回调地址{}", result);
        return result;
    }



    public static String handelWifiYcxyReplace( ReportDTO reportDTO,AccountDTO accountDTO,WifiRequest wifiRequest) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("platform", "ANDROID");
        map.put("ts_sec", System.currentTimeMillis());
        map.put("click_id", reportDTO.getBizRequestId());
        map.put("ip", reportDTO.getIp());
        map.put("oaid", reportDTO.getOaid());
        if (StringUtils.isNotBlank(reportDTO.getOaid())) {
            map.put("oaid_sum", SecureUtil.md5(reportDTO.getOaid()));
        }
        map.put("idfa", "");
        map.put("idfa_sum", "");
        map.put("imei_sum", StringUtils.isBlank(wifiRequest.getImeiMd5()) ? "" : wifiRequest.getImeiMd5());
        map.put("android_id_sum", reportDTO.getHash_android_id());
        map.put("model", "");
        if (StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            map.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());

        map.put("callback_url", allCabackUrl);
        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        String result = accountDTO.getDspReportUrl() + "&" + params;
        log.info("完整的回调地址{}", result);
        return result;
    }



    public static String qttUrl(String dspReportUrl, ReportDTO reportDTO, String dspCallbackUrl) {


        String extend = reportDTO.getExtend();
        QttRequest qttRequest = JSONObject.parseObject(extend, QttRequest.class);

        long time = System.currentTimeMillis();
        Map<String,String> map=new HashMap<>();
        map.put("REQUEST_ID",reportDTO.getBizRequestId());
        map.put("OAID",StringUtils.isNotBlank(qttRequest.getOaid())?qttRequest.getOaid():"");
        map.put("IP",reportDTO.getIp());
        map.put("OS","0");
        map.put("MTS",String.valueOf(time));
        map.put("IMEI",StringUtils.isNotBlank(qttRequest.getImei())?qttRequest.getImei():"");
        map.put("IMEI_MD5",StringUtils.isNotBlank(qttRequest.getImeiMd5())?qttRequest.getImeiMd5():"");
        map.put("OAID_MD5",StringUtils.isNotBlank(qttRequest.getOaidMd5())?qttRequest.getOaidMd5():"");
        map.put("IDFA","");
        map.put("IDFA_MD5","");
        map.put("UA",URLUtil.encode(reportDTO.getUser_agent()));
        map.put("MODEL","");
        map.put("MAC","");
        map.put("MAC_MD5","");
        map.put("MAC1_MD5","");
        map.put("CAID","");
        map.put("TRAFFIC_SOURCE","");



        String allCabackUrl = String.format(dspCallbackUrl, reportDTO.getBizRequestId());

        map.put("CALLBACK_URL",allCabackUrl);
        log.info("完整的回调地址{}",allCabackUrl);
        return   MacroReplacement.replaceMacros(dspReportUrl,map);
    }

    /**
     * URL 进行宏替换
     * @param adverUrl 借款url
     * @param reportDTO 请求参数
     * @return 返回替换后的完整url
     */
    public static String handMarcroReplace(String adverUrl, ReportDTO reportDTO,String str) {



        long time = System.currentTimeMillis();
        Map<String,String> map=new HashMap<>();
        map.put("REQUEST_ID",reportDTO.getBizRequestId());
        map.put("OAID",reportDTO.getOaid());
        map.put("IP",reportDTO.getIp());
        map.put("OS","0");
        map.put("MTS",String.valueOf(time));
        if(StringUtils.isNotBlank(reportDTO.getImeiSum())) {
            map.put("IMEI_MD5", reportDTO.getImeiSum());
        }else{
            map.put("IMEI_MD5",reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum())) {
            map.put("OAID_MD5", reportDTO.getOaidSum());
        }
        map.put("IDFA","");
        map.put("IDFA_MD5","");
        map.put("UA","");
        map.put("MODEL","");
        map.put("MAC","");
        map.put("MAC_MD5","");
        map.put("MAC1_MD5","");
        map.put("CAID","");
        map.put("TRAFFIC_SOURCE","");



        String allCabackUrl = String.format(str, reportDTO.getBizRequestId());
        log.info("完整的回调地址{}",allCabackUrl);
        map.put("CALLBACK_URL",allCabackUrl);

        return   MacroReplacement.replaceMacros(adverUrl,map);
    }


    /**
     * URL 进行宏替换
     * @param adverUrl 借款url
     * @param reportDTO 请求参数
     * @return 返回替换后的完整url
     */
    public static String handKsjsMarcroReplace(String adverUrl, ReportDTO reportDTO,String str) {
        long time = System.currentTimeMillis();
        Map<String,String> map=new HashMap<>();
        map.put("REQUEST_ID",reportDTO.getBizRequestId());
        map.put("OAID","");
        map.put("IP",reportDTO.getIp());
        map.put("OS","0");
        map.put("MTS",String.valueOf(time));
        map.put("IMEI","");
        map.put("IMEI_MD5",reportDTO.getImei());
        map.put("OAID_MD5",reportDTO.getOaid());
        map.put("IDFA","");
        map.put("IDFA_MD5","");
        map.put("UA",URLUtil.encode(reportDTO.getUser_agent()));
        map.put("MODEL","");
        map.put("MAC","");
        map.put("MAC_MD5","");
        map.put("MAC1_MD5","");
        map.put("CAID","");
        map.put("TRAFFIC_SOURCE","");



        String allCabackUrl = String.format(str, reportDTO.getBizRequestId());

        map.put("CALLBACK_URL",allCabackUrl);
        log.info("完整的回调地址{}",allCabackUrl);
        return   MacroReplacement.replaceMacros(adverUrl,map);
    }


    /**
     * 追加签名
     * @param resultUrl
     * @param sign
     * @return
     */
    public static String handElmSalt(String resultUrl, String sign) {
     StringBuilder stringBuilder=new StringBuilder();
     stringBuilder.append(resultUrl);
     stringBuilder.append("&");
     stringBuilder.append("signature=");
     stringBuilder.append(sign);
     return stringBuilder.toString();
    }


    /**
     * 追加签名
     * @param resultUrl
     * @param sign
     * @return
     */
    public static String handMeitu(String resultUrl, String sign) {
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(resultUrl);
        stringBuilder.append("&sign=");
        stringBuilder.append(sign);
        return stringBuilder.toString();
    }

    /**
     * 组装完整的快手极速URL上报
     * @param accountDTO
     * @return
     */
    public static String handKsJsUrlImpUrl(AccountDTO accountDTO, ReportDTO reportDTO) {

        return convertUrlParams(accountDTO,reportDTO);
    }

    private static String convertUrlParams(AccountDTO accountDTO, ReportDTO reportDTO) {
        String dspExtend = accountDTO.getDspExtend();
        TencentDspExtend tencentDspExtend = JSON.parseObject(dspExtend, TencentDspExtend.class);
        log.info("-dspID {}---dspExtend--{}",reportDTO.getDspId(),dspExtend);

        String extend = reportDTO.getExtend();
        TencentReportRequest tencentReportRequest=JSON.parseObject(extend,TencentReportRequest.class);

        Map<String,Object> params=new HashMap<>();
        params.put("adid",tencentDspExtend.getAdid());
        params.put("imei", tencentReportRequest.getMuid());
        params.put("oaid", tencentReportRequest.getHash_oaid());
        params.put("android_id", tencentReportRequest.getHash_android_id());
        params.put("ip", reportDTO.getIp());
        params.put("user_agent", reportDTO.getUser_agent());
        if(reportDTO.getEventType().equals("click")){
            String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
            params.put("callback", allCabackUrl);
            params.put("click_id",tencentReportRequest.getClick_id());
        }

        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8);


       if(reportDTO.getEventType().equals("click")){
          //组装点击的url
            return  accountDTO.getDspReportClickUrl()+"?"+urlParams;
        }else{
           //组装点击的url
           return  accountDTO.getDspReportImpUrl()+"?"+urlParams;
        }
    }

    /**
     * 生成完整的小红书地址
     * @param reportDTO 参数
     * @param accountDTO 账户的配置信息
     * @return 地址URL
     */
    public static String handWifiUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        String dspExtend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(dspExtend);
        String channel = jsonObject.getString("channel");
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        log.info("wifi小红书回调地址为{}",allCabackUrl);

        String extend = reportDTO.getExtend();
        WifiRequest wifiRequest=JSONObject.parseObject(extend,WifiRequest.class);

        Map<String,Object> params=new HashMap<>();
        params.put("channel",channel);
        params.put("ts_sec",reportDTO.getTraceTime());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("callback_url",allCabackUrl);
        params.put("platform","ANDROID");
        if(StringUtils.isNotBlank(wifiRequest.getImeiMd5())) {
            params.put("imei_sum", wifiRequest.getImeiMd5().toLowerCase());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaid())) {
            params.put("oaid_sum", SecureUtil.md5(reportDTO.getOaid()).toLowerCase());
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getHash_android_id())) {
            params.put("android_id_sum", reportDTO.getHash_android_id().toLowerCase());
        }
        params.put("is_imp",false);
//        params.put("mac","__MAC__");
//        params.put("ip","__IP__");
//        params.put("ua","__UA__");
//        params.put("model","__MODEL__");
//        params.put("osv","__OSV__");
//        params.put("idfa_sum","__IDFA_SUM__");
//        params.put("caid0","__CAID0__");
//        params.put("mac_sum","__MAC_SUM__");



        String urParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        String url=replace(accountDTO.getDspReportUrl(),channel);
        return url+"?"+urParams;
    }



    /**
     * 生成完整的小红书地址
     * @param reportDTO 参数
     * @param accountDTO 账户的配置信息
     * @return 地址URL
     */
    public static String handMeituUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        String dspExtend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(dspExtend);
        String channel = jsonObject.getString("channel");
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        log.info("小红书回调地址为{}",allCabackUrl);

        Map<String,Object> params=new HashMap<>();
        params.put("channel",channel);
        params.put("ts_ms",reportDTO.getTraceTime());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("callback_url",allCabackUrl);
        params.put("platform","ANDROID");
        params.put("imei_sum",reportDTO.getImei());
        if(StringUtils.isNotBlank(reportDTO.getOaid())) {
            params.put("oaid_sum", SecureUtil.md5(reportDTO.getOaid()));
            params.put("oaid", reportDTO.getOaid());
        }
        params.put("android_id_sum",reportDTO.getHash_android_id());
        params.put("ip",reportDTO.getIp());
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent())?URLUtil.encode(reportDTO.getUser_agent()):"");

        String urParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        String url=replace(accountDTO.getDspReportUrl(),channel);
        return url+"?"+urParams;
    }



    public static String replace(String input, String placeholder, String replacement) {
        return input.replace(placeholder, replacement);
    }

    public static String replace(String input, String replacement) {
        return input.replace("{channel}", replacement);
    }

    public static String handKjbUrlImpUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        return convertKjbUrlParams(accountDTO,reportDTO);
    }

    public static String handHwKjbUrlImpUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        return convertHwKjbUrlParams(accountDTO,reportDTO);
    }


    private static String convertHwKjbUrlParams(AccountDTO accountDTO, ReportDTO reportDTO) {
        String dspExtend = accountDTO.getDspExtend();
       JSONObject jsonObject=JSONObject.parseObject(dspExtend);

       HwReportRequest hwReportRequest=JSONObject.parseObject(reportDTO.getExtend(),HwReportRequest.class);

        Map<String,Object> params=new HashMap<>();
        params.put("adid",jsonObject.getString("adid"));
        params.put("imei", "");
        params.put("oaid", reportDTO.getOaid());
        params.put("android_id", "");
        params.put("ip", hwReportRequest.getIp());
        params.put("user_agent", hwReportRequest.getUser_agent());

        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback", allCabackUrl);
        params.put("click_id",reportDTO.getBizRequestId());

        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8);


        if(reportDTO.getEventType().equals("click")){
            //组装点击的url
            return  accountDTO.getDspReportClickUrl()+"?"+urlParams;
        }else{
            //组装点击的url
            return  accountDTO.getDspReportImpUrl()+"?"+urlParams;
        }
    }




    private static String convertKjbUrlParams(AccountDTO accountDTO, ReportDTO reportDTO) {
        String dspExtend = accountDTO.getDspExtend();
        TencentDspExtend tencentDspExtend = JSON.parseObject(dspExtend, TencentDspExtend.class);


        Map<String,Object> params=new HashMap<>();
        params.put("adid",tencentDspExtend.getAdid());
        params.put("imei", reportDTO.getImei());
        params.put("oaid", reportDTO.getOaid());
        params.put("android_id", reportDTO.getHash_android_id());
        params.put("ip", reportDTO.getIp());
        params.put("user_agent", reportDTO.getUser_agent());
        if(reportDTO.getEventType().equals("click")){
            String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
            params.put("callback", allCabackUrl);
            params.put("click_id",reportDTO.getBizRequestId());
        }

        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8);


        if(reportDTO.getEventType().equals("click")){
            //组装点击的url
            return  accountDTO.getDspReportClickUrl()+"?"+urlParams;
        }else{
            //组装点击的url
            return  accountDTO.getDspReportImpUrl()+"?"+urlParams;
        }
    }










    public static String convertYouYouKjbUrlParams(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("click_id", reportDTO.getBizRequestId());
        if (StringUtils.isNotBlank(reportDTO.getImei()) && !reportDTO.getImei().equals("__IMEI__")) {
            params.put("imei", reportDTO.getImei());
        } else {
            params.put("imei", "");
        }
        params.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        params.put("android_id", StringUtils.isNotBlank(reportDTO.getHash_android_id()) ? reportDTO.getHash_android_id() : "");
        params.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        params.put("user_agent", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback", allCabackUrl);
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + urlParams;

    }



    public static String covertParamsOrderByAsc(Map<String, Object> param) {
        StringBuilder paramStr = new StringBuilder();

        Map<String, Object> paramSort = new TreeMap<>();

        //默认排序，按照ASCII升序
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            if (entry.getValue() == null) {
                continue;
            }
            paramSort.put(entry.getKey(), entry.getValue());
        }

        // 拼接字符串
        for (Map.Entry<String, Object> entry : paramSort.entrySet()) {
            paramStr.append("&").append(entry.getKey()).append("=").append(entry.getValue());
        }
        if (paramStr.length() > 0) {
            paramStr.deleteCharAt(0);
        }

        return paramStr.toString();
    }




    public static String viVoCovert(String dspReportUrl, ReportDTO reportDTO, String dspCallbackUrl) {
        String extend = reportDTO.getExtend();
        ViVoRequest viVoRequest = JSONObject.parseObject(extend, ViVoRequest.class);

        long time = System.currentTimeMillis();
        Map<String,String> map=new HashMap<>();
        map.put("REQUEST_ID",reportDTO.getBizRequestId());
        map.put("OAID",StringUtils.isNotBlank(viVoRequest.getOaid())?viVoRequest.getOaid():"");
        map.put("IP",reportDTO.getIp());
        map.put("OS","0");
        map.put("MTS",String.valueOf(time));
        map.put("IMEI","");
        map.put("IMEI_MD5",StringUtils.isNotBlank(viVoRequest.getImei())?viVoRequest.getImei():"");
        map.put("OAID_MD5",StringUtils.isNotBlank(viVoRequest.getOaidMd5())?viVoRequest.getOaidMd5():"");
        map.put("IDFA","");
        map.put("IDFA_MD5","");
        map.put("UA",URLUtil.encode(reportDTO.getUser_agent()));
        map.put("MODEL","");
        map.put("MAC","");
        map.put("MAC_MD5","");
        map.put("MAC1_MD5","");
        map.put("CAID","");
        map.put("TRAFFIC_SOURCE","");



        String allCabackUrl = String.format(dspCallbackUrl, reportDTO.getBizRequestId());

        map.put("CALLBACK_URL",allCabackUrl);
        log.info("完整的回调地址{}",allCabackUrl);
        return   MacroReplacement.replaceMacros(dspReportUrl,map);
    }












    public static String covertKsLmUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        JSONObject jsonObject = JSONObject.parseObject(accountDTO.getDspExtend());
        String channel = jsonObject.getString("channel");
        String advertisingSpaceId=jsonObject.getString("advertisingSpaceId");
        String taskId=jsonObject.getString("taskId");
        String adAgent=jsonObject.getString("adAgent");
        String callBackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());
        Map<String,Object> prams=new HashMap<>();
        prams.put("channel",channel);
        prams.put("advertisingSpaceId",advertisingSpaceId);
        prams.put("taskId",taskId);
        prams.put("adAgent",adAgent);
        prams.put("app","45");
        prams.put("adid",reportDTO.getCampaignId());
        prams.put("cid",reportDTO.getContentId());
        prams.put("adGroupId",reportDTO.getAdgroupId());
        prams.put("imeiMd5",reportDTO.getImei());
        prams.put("oaid",reportDTO.getOaid());
        if (StringUtils.isNotBlank(reportDTO.getOaid())){
            prams.put("oaidMd5", SecureUtil.md5(reportDTO.getOaid()));
        }
        prams.put("callbackUrl", URLEncoderDecoderTool.encodeURL(callBackUrl));
        String paramsUrl= URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"&"+paramsUrl;
    }





    public static String covertKsJyUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("account_id", "");
        params.put("campaignId", "");
        params.put("unitId", "");
        params.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        params.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        params.put("ip", reportDTO.getIp());
        params.put("os", "android");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + urlParams;
    }






    public static String covertHHUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "0");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp())?reportDTO.getIp():"");
        prams.put("tms", reportDTO.getTraceTime());
        if (StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            prams.put("adAgent", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }
        prams.put("imeiMd5", StringUtils.isNotBlank(reportDTO.getImei())?reportDTO.getImei():"");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid())?reportDTO.getOaid():"");
        if (StringUtils.isNotBlank(reportDTO.getOaid())){
            prams.put("oaidMd5", SecureUtil.md5(reportDTO.getOaid()));
        }
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        prams.put("callbackUrl", URLEncoderDecoderTool.encodeURL(callBackUrl));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }



    public static String covertKsHHUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "0");
        prams.put("ip", reportDTO.getIp());
        prams.put("tms", reportDTO.getTraceTime());
        if (StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            prams.put("adAgent", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }
        prams.put("imeiMd5", StringUtils.isNotBlank(reportDTO.getImei())?reportDTO.getImei():"");
        prams.put("oaidMd5", StringUtils.isNotBlank(reportDTO.getOaid())?reportDTO.getOaid():"");
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        prams.put("callbackUrl", URLEncoderDecoderTool.encodeURL(callBackUrl));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }


    /**
     * 只有优优使用
     * @param accountDTO
     * @param reportDTO
     * @return
     */
    public static String covertYYJymUrl(AccountDTO accountDTO, ReportDTO reportDTO, YouYouRequest youYouRequest) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "android");
        prams.put("ip", StringUtils.isNotBlank(youYouRequest.getIp()) ? youYouRequest.getIp() : "");
//        prams.put("ua", StringUtils.isNotBlank(youYouRequest.getUa()) ? URLUtil.encode(youYouRequest.getUa()) : "");
        prams.put("model", "");
        prams.put("ts",youYouRequest.getTimestamp());
        if (StringUtils.isNotBlank(youYouRequest.getImei()) && !youYouRequest.getImei().equals("__IMEI__")) {
            prams.put("imei", youYouRequest.getImei());
            prams.put("imeiMd5",  SecureUtil.md5(youYouRequest.getImei()));
        }else {
            prams.put("imei", "");
            prams.put("imeiMd5", "");
        }
        prams.put("clickId",reportDTO.getBizRequestId());
        prams.put("oaid", StringUtils.isNotBlank(youYouRequest.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaidMd5", StringUtils.isNotBlank(youYouRequest.getOaidMd5()) ? youYouRequest.getOaidMd5() : "");
        prams.put("callback", URLUtil.encode(accountDTO.getDspCallbackUrl()));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }


    /**
     * 只有优优使用
     * @param accountDTO
     * @param reportDTO
     * @return
     */
    public static String covertYYWpzUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "1");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum())?reportDTO.getImeiSum():"");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaid_md5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        prams.put("callback", URLUtil.encode(accountDTO.getDspCallbackUrl()));
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        prams.put("callback", URLEncoderDecoderTool.encodeURL(callBackUrl));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }



    /**
     * 只有优优使用
     * @param accountDTO
     * @param reportDTO
     * @return
     */
    public static String covertSfkjJymUrl(AccountDTO accountDTO, ReportDTO reportDTO, SfkjRequest sfkjRequest) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "android");
        prams.put("ip", StringUtils.isNotBlank(sfkjRequest.getIp()) ? sfkjRequest.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(sfkjRequest.getUa()) ? URLUtil.encode(sfkjRequest.getUa()) : "");
        prams.put("model", "");
        prams.put("ts",sfkjRequest.getTime());
        prams.put("imei", StringUtils.isNotBlank(sfkjRequest.getImei()) ? sfkjRequest.getImei() : "");
        prams.put("imeiMd5", StringUtils.isNotBlank(sfkjRequest.getImeiSum()) ? sfkjRequest.getImeiSum() : "");

        prams.put("clickId",reportDTO.getBizRequestId());
        prams.put("oaid", StringUtils.isNotBlank(sfkjRequest.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaidMd5", StringUtils.isNotBlank(sfkjRequest.getOaidSum()) ? sfkjRequest.getOaidSum() : "");
        prams.put("callback", URLUtil.encode(accountDTO.getDspCallbackUrl()));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }


    public static String covertAllJymUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "android");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
//        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("model", "");
        prams.put("ts",System.currentTimeMillis());
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imeiMd5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("clickId",reportDTO.getBizRequestId());
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaidMd5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        prams.put("callback", URLUtil.encode(accountDTO.getDspCallbackUrl()));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }





    public static String covertAllXuanYiUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("idfa", "");
        prams.put("idfa_md5", "");
        prams.put("caid", "");
        prams.put("android_id", StringUtils.isNotBlank(reportDTO.getHash_android_id()) ? reportDTO.getHash_android_id() : "");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("model", "");
        prams.put("ts",System.currentTimeMillis());
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("request_id",reportDTO.getBizRequestId());
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaid_md5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(prams, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }




    /**
     * 只有优优使用
     * @param accountDTO
     * @param reportDTO
     * @return
     */
    public static String covertHwJymUrl(AccountDTO accountDTO, ReportDTO reportDTO, HwReportRequest hwReportRequest) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "android");
        prams.put("ip", StringUtils.isNotBlank(hwReportRequest.getIp()) ? hwReportRequest.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(hwReportRequest.getUser_agent()) ? URLUtil.encode(hwReportRequest.getUser_agent()) : "");
        prams.put("model", "");
        prams.put("ts",System.currentTimeMillis());
        prams.put("imei", StringUtils.isNotBlank(hwReportRequest.getId_type()) ? hwReportRequest.getId_type() : "");
        prams.put("imeiMd5", "");
        prams.put("clickId",reportDTO.getBizRequestId());
        prams.put("oaid", StringUtils.isNotBlank(hwReportRequest.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaidMd5", StringUtils.isNotBlank(hwReportRequest.getOaid()) ? SecureUtil.md5(hwReportRequest.getOaid()) : "");
        prams.put("callback", URLUtil.encode(accountDTO.getDspCallbackUrl()));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }


    /**
     * 鲸久
     * 这里要求获取到的是imei
     * @param accountDTO
     * @param reportDTO
     * @return
     */
    public static String covertJJUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "android");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("model", "");
        prams.put("clickId",reportDTO.getBizRequestId());
        prams.put("ts",System.currentTimeMillis());
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imeiMd5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaidMd5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        prams.put("callback", URLUtil.encode(callBackUrl));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }


    public static String covertYouYouUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "1");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("ts",System.currentTimeMillis());
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaid_md5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(prams, StandardCharsets.UTF_8) + "&callbackUrl=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }

    public static String covertLiTianUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        String dspExtend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(dspExtend);
        String access_id = jsonObject.getString("access_id");
        String secret = jsonObject.getString("secret");
        long ts = System.currentTimeMillis() / 1000;
        Map<String, Object> pramsSigMap = new HashMap<>();
        pramsSigMap.put("access_id", access_id);
        pramsSigMap.put("ts", ts);
        String signature = URLUtil.buildQuery(pramsSigMap, StandardCharsets.UTF_8) + secret;
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "0");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("ts", ts);
        prams.put("signature", signature);
        prams.put("access_id", access_id);
        prams.put("request_id", reportDTO.getBizRequestId());
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaid_md5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(prams, StandardCharsets.UTF_8) + "&callback_url=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }

    public static String covertYuYueUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "2");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imeimd5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaidmd5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(prams, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }

}
